// page/mine/pages/applyAgent/applyAgent.js 申请加入合伙人
var app = getApp();

var that;
Page({

      /**
       * 页面的初始数据
       */
      data: {
        flag: true,
        checked: false,
        Invitation: true,
        agent: '',
        companydm: '',
        referee: null,
      },
      checkboxChange(e) {
        this.setData({
          checked: !this.data.checked,
          disabled: true
        })
      },
      formSubmit(e) { //提交申请成为合伙人
        that = this
        console.log(e.detail.value)
        // wx.setStorageSync('applayAgentname', e.detail.value.username)
        // wx.setStorageSync('applayAgentphone', e.detail.value.phone)
        console.log(11)

        //表单校验start

        var userName = e.detail.value.username;
        var mobile = e.detail.value.phone;
        var phonetel = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
        if (!(/^[\u4E00-\u9FA5A-Za-z]+$/.test(userName))) {
          wx.showToast({
            title: '姓名有误',
            icon: true,
            duration: 1000,
          })
          return false
        } else if (mobile == '') {
          wx.showToast({
            title: '手机号不能为空',
          })

          return false
        } else if (mobile.length != 11) {
          wx.showToast({
            title: '手机号长度有误！',
            icon: 'success',
            duration: 1500
          })
          return false;
        } else if (!phonetel.test(mobile)) {
          wx.showToast({
            title: '手机号有误！',
            icon: 'success',
            duration: 1500
          })
          return false;
        } else if (this.data.checked !== true) {
          wx.showModal({
            title: '提示',
            content: '请阅读申请倡议书并勾选按钮',
          })
        } else {
          var hyid = wx.getStorageSync("bizUserInfo").id
          wx.request({
            url: app.api.applyPartner() + `?id=${hyid}&companyId=${app.globalData.companyId}`,
            // url: app.api.applyPartner() + `?id=26`,
            method: 'POST',
            data: {
              name: e.detail.value.username,
              phone: e.detail.value.phone
            },
            success: function(res) {
              if (res.statusCode == 200 && res.errMsg == 'request:ok') {
                console.log(res)
                if (res.data.code == 1) {
                  wx.showToast({
                    title: '等待审核',
                    icon: 'success',
                    duration: 2000
                  })
                  wx.request({
                    //memberId=26
                    //memberId=${wx.getStorageSync("bizUserInfo").id}
                    url: app.api.distpartnerinfo() + `?memberId=${hyid}`,
                    // url: app.api.distpartnerinfo() + `?memberId=26`,
                    success: function(res) {
                      console.log(res.data.data.referee)
                      wx.setStorageSync('companyName', res.data.data.companyName)
                      if (res.statusCode == 200 && res.errMsg == 'request:ok') {
                        if (res.data.code == 1) {
                          // wx.navigateTo({
                          //   url: '/page/mine/pages/partner/partner',
                          // })
                          //审核状态判断 
                          if (res.data.data !== null) {
                            if (res.data.data.state == 0) {
                              wx.showToast({
                                title: '等待审核',
                                icon: 'success',
                                duration: 2000
                              })
                            } else if (res.data.data.state == 1) {
                              wx.showToast({
                                title: '审核通过',
                                icon: 'success'
                              })
                              setTimeout(function() {
                                wx.navigateTo({
                                  url: '/page/mine/pages/partner/partner',
                                })
                              }, 1000)
                            } else {
                              wx.showToast({
                                title: '审核失败',
                                icon: 'success',
                                duration: 2000
                              })
                            }
                          }

                        }
                      } else {
                        wx.showToast({
                          title: res.data.message,
                        })
                      }
                    }

                  })
                } else {
                  wx.showToast({
                    title: res.data.message,
                  })
                }
              } else {
                wx.showToast({
                  title: res.data.message,
                })
              }
            }
          })
        }

      },
      /**
       * 生命周期函数--监听页面加载
       */
      onLoad: function(options) {
          that = this
          that.setData({
            // companyName: wx.getStorageSync('companyName'),
            companydm: app.globalData.shopData.name
          })
          wx.request({
            //memberId=26
            //memberId=${wx.getStorageSync("bizUserInfo").id}
            url: app.api.distpartnerinfo() + `?memberId=${wx.getStorageSync("bizUserInfo").id}`,
            // url: app.api.distpartnerinfo() + `?memberId=26`,
            success: function(res) {
              console.log(res.data,'datares')
              if(res.data.data !== null){
                console.log(res.data.data.referee)
                that.setData({
                  referee: res.data.data.referee,
                })
              }
            }
          })
          },
            //弹框出现 申请书
            proposal: function() {
              that = this
              wx.request({
                url: app.api.ApplicationUser(),
                method: 'POST',
                success: function(res) {
                  console.log(res.data)
                  if (res.statusCode == 200 && res.errMsg == 'request:ok') {
                    if (res.data.code == 1) {
                      console.log(res.data.data)
                      console.log(res.data.data.data)
                      that.setData({
                        agent: res.data.data[0],
                      })
                    }
                  } else {
                    wx.showToast({
                      title: res.data.message,
                    })
                  }
                }
              })
              that.setData({
                flag: false
              })
            },
            //消失

            hide: function() {

              this.setData({
                flag: true
              })

            },
            /**
             * 生命周期函数--监听页面初次渲染完成
             */
            onReady: function() {

            },

            /**
             * 生命周期函数--监听页面显示
             */
            onShow: function() {},

            /**
             * 生命周期函数--监听页面隐藏
             */
            onHide: function() {

            },

            /**
             * 生命周期函数--监听页面卸载
             */
            onUnload: function() {

            },

            /**
             * 页面相关事件处理函数--监听用户下拉动作
             */
            onPullDownRefresh: function() {

            },

            /**
             * 页面上拉触底事件的处理函数
             */
            onReachBottom: function() {

            },

            /**
             * 用户点击右上角分享
             */
            onShareAppMessage: function() {

            }
          })